<template>
  <div class="app-container">
    <el-button type="primary" @click="addCategory">添加新的菜品类别</el-button>
    <br>
    <br>
    <el-table :data="categoryList" stripe border style="width: 500px">
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="名称" prop="name" align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row,$index}">
          <!-- <el-button size="mini" @click="upadateCategory(row)">编辑</el-button> -->
          <el-button type="danger" size="mini" @click="deleteCategory(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getCategory, deleteCategory } from '@/api/category'

export default {
  data() {
    return {
      categoryList: null,
      categoryListLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // this.$message('查询全部菜类')
      this.listLoading = true
      getCategory().then(res => {
        this.categoryList = res.data
        this.categoryListLoading = false
      })
    },
    deleteCategory(category, index) {
      this.$confirm('是否要进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCategory(category.id).then(res => {
          this.$message.success('成功删除')
          this.fetchData()
        })
      })
    },
    upadateCategory(row) {
      this.$router.push({ path: '/category/updateCategory', query: { id: row.id }})
    },
    addCategory() {
      this.$router.push({ path: '/category/addCategory' })
    }
  }
}
</script>

<style scoped>
</style>
